package com.example.myapplication;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Stack;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity{
//    Stack<Double> st;
    char op = '.';
    Double last = 0.0;
    Double next = 0.0;
    public void calculate(char op, Double se){
        Double num;
        Double newnum;
        switch (op){
            case '/':
                Log.e("aa", "d");
                last = last / se;
                Log.e("div", last + "");
                break;
            case 'X':
                Log.e("aa", "d");
                last = last * se;
                Log.e("div", last + "");
                break;
            case '—':
                Log.e("aa", "d");
                last = last - se;
                Log.e("div", last + "");
                break;
            case '+':
                Log.e("aa", "d");
                last = last + se;
                Log.e("div", last + "");
                break;
            default:
                last = se;
                Log.e("aa", "h" + se);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        Log.e("TAG", "onCreate: ");
        setContentView(R.layout.layout);
        //由于在输入为空时要不做任何反应，但是找不到任何对照，特地将textview设为空的对照
        TextView textView = (TextView)findViewById(R.id.Text);
        TextView textview = (TextView)findViewById(R.id.text);
        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = (String) textView.getText() + '1';
                textView.setText(s);
            }
        });

        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = (String) textView.getText() + '2';
                textView.setText(s);
            }
        });

        Button button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = (String) textView.getText() + '3';
                textView.setText(s);
            }
        });

        Button button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = (String) textView.getText() + '4';
                textView.setText(s);
            }
        });

        Button button5 = (Button) findViewById(R.id.button5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = (String) textView.getText() + '5';
                textView.setText(s);
            }
        });

        Button button6 = (Button) findViewById(R.id.button6);
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = (String) textView.getText() + '6';
                textView.setText(s);
            }
        });

        Button button7 = (Button) findViewById(R.id.button7);
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = (String) textView.getText() + '7';
                textView.setText(s);
            }
        });

        Button button8 = (Button) findViewById(R.id.button8);
        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = (String) textView.getText() + '8';
                textView.setText(s);
            }
        });

        Button button9 = (Button) findViewById(R.id.button9);
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = (String) textView.getText() + '9';
                textView.setText(s);
            }
        });

        Button button0 = (Button) findViewById(R.id.button0);
        button0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = (String) textView.getText() + '0';
                textView.setText(s);
            }
        });

        Button button_point = (Button) findViewById(R.id.buttonpoint);
        button_point.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(textView.getText().toString().contains("."))
                    return;
                String s = (String) textView.getText() + '.';
                textView.setText(s);
            }
        });

        Button div = (Button) findViewById(R.id.div);
        div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Double se = Double.parseDouble(textView.getText().toString());
                if(textView.getText().toString() == textview.getText().toString()) {
                    last = se;
                    return;
                }
//                Log.e("aa", "a" + textView.getText().toString() + "a");
//                Log.e("aa", "a");
                calculate(op, se);
//                Log.e("aa", "b");
                op = '/';
                textView.setText("");
//                Log.e("aa", "c");

            }
        });

        Button multiply = (Button) findViewById(R.id.multi);
        multiply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Double se = Double.parseDouble(textView.getText().toString());
                if(textView.getText().toString() == textview.getText().toString()) {
                    last = se;
                    return;
                }
//                Log.e("aa", "a" + textView.getText().toString() + "a");
                Log.e("aa", "a");
                calculate(op, se);
                Log.e("aa", "b");
                op = 'X';
                textView.setText("");
                Log.e("aa", "c");
            }
        });

        Button plus = (Button) findViewById(R.id.plus);
        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Double se = Double.parseDouble(textView.getText().toString());
                if(textView.getText().toString() == textview.getText().toString()) {
                    last = se;
                    return;
                }
//                Log.e("aa", "a" + textView.getText().toString() + "a");
//                Log.e("aa", "a");
                calculate(op, se);
//                Log.e("aa", "b");
                op = '+';
                textView.setText("");
//                Log.e("aa", "c");

            }
        });

        Button sub = (Button) findViewById(R.id.subtract);
        sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Double se = Double.parseDouble(textView.getText().toString());
                if(textView.getText().toString() == textview.getText().toString()) {
                    last = se;
                    return;
                }
//                Log.e("aa", "a" + textView.getText().toString() + "a");
//                Log.e("aa", "a");
                calculate(op, se);
//                Log.e("aa", "b");
                op = '—';
                textView.setText("");
//                Log.e("aa", "c");

            }
        });

        Button ce = (Button) findViewById(R.id.buttonCE);
        ce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText("");
                last = 0.0;
            }
        });

        Button c = (Button) findViewById(R.id.buttonC);
        c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText("");
            }
        });

        Button back = (Button)findViewById(R.id.buttonback);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = textView.getText().toString();
                s.subSequence(0, s.length() - 1);
                textView.setText(s.subSequence(0, s.length() - 1));
            }
        });

        Button equal = (Button) findViewById(R.id.buttonequal);
        equal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Double se = Double.parseDouble(textView.getText().toString());
                calculate(op, se);
                op='.';
                //op
                textView.setText(last.toString());
            }
        });
//        View.OnClickListener listener = new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Log.e("uuu", "onClick: ");
//                if (view.equals(button1)) {
//                    String s = (String) textView.getText() + '1';
//                    textView.setText(s);
//                    Log.e("ooo", s);
//                }
//                else if(view.equals(button2)){
//                    TextView text = (TextView) findViewById(R.id.Text);
//                    String s = (String) text.getText() + '2';
//                    text.setText(s);
//                }
//                else if(view.equals(button3)){
//                    TextView text = (TextView) findViewById(R.id.Text);
//                    String s = (String) text.getText() + '3';
//                    text.setText(s);
//                }
//                else if(view.equals(button4)){
//                    TextView text = (TextView) findViewById(R.id.Text);
//                    String s = (String) text.getText() + '4';
//                    text.setText(s);
//
//                }
//                else if(view.equals(button5)){
//                    TextView text = (TextView) findViewById(R.id.Text);
//                    String s = (String) text.getText() + '5';
//                    text.setText(s);
//                }
//                else if(view.equals(button6)){
//                    TextView text = (TextView) findViewById(R.id.Text);
//                    String s = (String) text.getText() + '6';
//                    text.setText(s);
//                }
//                else if(view.equals(button7)){
//                    TextView text = (TextView) findViewById(R.id.Text);
//                    String s = (String) text.getText() + '7';
//                    text.setText(s);
//                }
//                else if(view.equals(button8)){
//                    TextView text = (TextView) findViewById(R.id.Text);
//                    String s = (String) text.getText() + '8';
//                    text.setText(s);
//                }
//                else if(view.equals(button9)){
//                    TextView text = (TextView) findViewById(R.id.Text);
//                    String s = (String) text.getText() + '9';
//                    text.setText(s);
//                }
//                else if(view.equals(button0)){
//                    TextView text = (TextView) findViewById(R.id.Text);
//                    String s = (String) text.getText() + '0';
//                    text.setText(s);
//                }
//                else if(view.equals(button_point)){
//                    TextView text = (TextView) findViewById(R.id.Text);
//                    String s = (String) text.getText() + '.';
//                    text.setText(s);
//                }
//                else if(view.equals(div)){
//                }
//                else if(view.equals(multiply)){
//                }
//                else if(view.equals(sub)){
//                }
//                else if(view.equals(plus)){
//                }
            }
        };
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
//            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
//            return insets;
//        });
//    }
//
//    @Override
//    public void onClick(View view) {
//            Log.e("uuu", "onClick: ");
//            if (view.equals(R.id.button1)) {
//                String s = (String) textView.getText() + '1';
//                textView.setText(s);
//                Log.e("ooo", s);
//            }
//            else if(view.equals(button2)){
//                TextView text = (TextView) findViewById(R.id.Text);
//                String s = (String) text.getText() + '2';
//                text.setText(s);
//            }
//            else if(view.equals(button3)){
//                TextView text = (TextView) findViewById(R.id.Text);
//                String s = (String) text.getText() + '3';
//                text.setText(s);
//            }
//            else if(view.equals(button4)){
//                TextView text = (TextView) findViewById(R.id.Text);
//                String s = (String) text.getText() + '4';
//                text.setText(s);
//
//            }
//            else if(view.equals(button5)){
//                TextView text = (TextView) findViewById(R.id.Text);
//                String s = (String) text.getText() + '5';
//                text.setText(s);
//            }
//            else if(view.equals(button6)){
//                TextView text = (TextView) findViewById(R.id.Text);
//                String s = (String) text.getText() + '6';
//                text.setText(s);
//            }
//            else if(view.equals(button7)){
//                TextView text = (TextView) findViewById(R.id.Text);
//                String s = (String) text.getText() + '7';
//                text.setText(s);
//            }
//            else if(view.equals(button8)){
//                TextView text = (TextView) findViewById(R.id.Text);
//                String s = (String) text.getText() + '8';
//                text.setText(s);
//            }
//            else if(view.equals(button9)){
//                TextView text = (TextView) findViewById(R.id.Text);
//                String s = (String) text.getText() + '9';
//                text.setText(s);
//            }
//            else if(view.equals(button0)){
//                TextView text = (TextView) findViewById(R.id.Text);
//                String s = (String) text.getText() + '0';
//                text.setText(s);
//            }
//            else if(view.equals(button_point)){
//                TextView text = (TextView) findViewById(R.id.Text);
//                String s = (String) text.getText() + '.';
//                text.setText(s);
//            }
//            else if(view.equals(div)){
//            }
//            else if(view.equals(multiply)){
//            }
//            else if(view.equals(sub)){
//            }
//            else if(view.equals(plus)){
//            }
//        }
//    }
//}